<?php
namespace Home\Controller;
use Think\Controller;
class ConmonController extends Controller {


        public function _initialize(){
            //查询前台顶级导航
            $top_nav = D('nav')->where('is_home=1')->select();

            // 查询分类
            $cate = $this->getCate();
            // print_r($cate);die;

            //动态实例化对应的数据库
             if(CONTROLLER_NAME!='Index'){
                 $this->MODEL=D(CONTROLLER_NAME);
             }

            $this->assign('top_nav',$top_nav);
            $this->assign('cate',$cate);


        }

        //递归查询分类方法，缺点：耗资源，三级别可以直接查询三次
        public function getCate($pid=0,$cate = array()){

                $cate = D('goods_cate')->where("pid=$pid")->select();
                // print_r($cate);die;
                foreach($cate as $k=>$v){

                    $cate[$k]['child'] = $this->getCate($v['id'],$cate);
                    // print_r($cate);die;
                }

                return $cate;

        }





}